package com.cabrida.livestar.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;

/**
 */
public class BarUtil {
	/**
	 * 设置状态栏图标为深色和魅族特定的文字风格 可以用来判断是否为Flyme用户
	 * 
	 * @param window
	 *            需要设置的窗口
	 * @param dark
	 *            是否把状态栏字体及图标颜色设置为深色
	 * @return boolean 成功执行返回true
	 * 
	 */
	public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
		boolean result = false;
		if (window != null) {
			try {
				WindowManager.LayoutParams lp = window.getAttributes();
				Field darkFlag = WindowManager.LayoutParams.class
						.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
				Field meizuFlags = WindowManager.LayoutParams.class
						.getDeclaredField("meizuFlags");
				darkFlag.setAccessible(true);
				meizuFlags.setAccessible(true);
				int bit = darkFlag.getInt(null);
				int value = meizuFlags.getInt(lp);
				if (dark) {
					value |= bit;
				} else {
					value &= ~bit;
				}
				meizuFlags.setInt(lp, value);
				window.setAttributes(lp);
				result = true;
			} catch (Exception e) {

			}
		}
		return result;
	}
	
	public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
		  Class<? extends Window> clazz = activity.getWindow().getClass();
		  try {
		    int darkModeFlag = 0;
		    Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
		    Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
		    darkModeFlag = field.getInt(layoutParams);
		    Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
		    extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
		    return true;
		  } catch (Exception e) {
		    e.printStackTrace();
		  }
		  return false;
		}
}
